/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.component;

import java.util.ArrayList;
import java.util.List;

import com.cleangwt.user.shared.Option;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class InterestingCell extends AbstractCell<List<String>> {
  interface Template extends SafeHtmlTemplates {
    @Template("<option value=\"{1}\">{0}</option>")
    SafeHtml deselected(String option, String value);

    @Template("<option value=\"{1}\" selected=\"selected\">{0}</option>")
    SafeHtml selected(String option, String value);
  }

  private static Template template;
  private final List<Option> options;

  /**
   * Construct a new {@link MultiOptionCell} with the specified options.
   * 
   * @param options the options in the cell
   */
  public InterestingCell(List<Option> options) {
    super(BrowserEvents.CLICK);
    this.options = options;
    if (template == null) {
      template = GWT.create(Template.class);
    }
  }

  @Override
  public void onBrowserEvent(Context context, Element parent, List<String> value,
      NativeEvent event, ValueUpdater<List<String>> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    String type = event.getType();
    if (BrowserEvents.CLICK.equals(type)) {
      List<String> newValue = new ArrayList<String>();
      for (int i = 0; i < parent.getChildCount(); i++) {
        SelectElement select = parent.getChild(i).cast();
        String selectedValue = select.getOptions().getItem(select.getSelectedIndex()).getValue();
        if (!"".equals(selectedValue)) {
          newValue.add(selectedValue);
        }

      }
      if (valueUpdater != null) {
        valueUpdater.update(newValue);
      }
    }
  }

  @Override
  public void render(Context context, List<String> value, SafeHtmlBuilder sb) {
    sb.appendHtmlConstant("<select style='display:inline; width:auto;' tabindex=\"-1\">");
    boolean hasValue1 = value != null && value.size() > 0;
    boolean hasValue2 = value != null && value.size() > 1;

    for (Option option : options) {
      if (hasValue1 && value.get(0).equals(option.getValue())) {
        sb.append(template.selected(option.getName(), option.getValue()));
      } else {
        sb.append(template.deselected(option.getName(), option.getValue()));
      }
    }
    sb.appendHtmlConstant("</select>");
    //
    sb.appendHtmlConstant("<select style='display:inline; width:auto;' tabindex=\"-1\">");
    for (Option option : options) {
      if (hasValue2 && value.get(1).equals(option.getValue())) {
        sb.append(template.selected(option.getName(), option.getValue()));
      } else {
        sb.append(template.deselected(option.getName(), option.getValue()));
      }
    }
    sb.appendHtmlConstant("</select>");
  }
}
